package com.qdsg.ysg.doctor.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @time 2020/11/16 10:21
 * Created by lbj on 2020/11/16 10:21.
 */
public class UserInfoEditActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.edt_userinfo)
    EditText edtUserinfo;
    private int editTypeInt = 0;


    @Override
    protected void initViews() {


    }

    @Override
    protected void initData() {
        String editType = getIntent().getStringExtra( "editType" );
        String editData = getIntent().getStringExtra( "editData" );

        if (!UtilString.isEmpty( editData )) {
            edtUserinfo.setText( editData );
        }
        if (!UtilString.isEmpty( editType )) {
            if ("name".equals( editType )) {
                tvTitle.setText( "姓名" );
                if (UtilString.isEmpty( editData )) {
                    edtUserinfo.setHint( "请输入姓名" );
                }
                editTypeInt = 1;
            } else if ("introduction".equals( editType )) {
                tvTitle.setText( "个人简介" );
                if (UtilString.isEmpty( editData )) {
                    edtUserinfo.setHint( "请输入个人简介" );
                }
                editTypeInt = 2;
            } else if ("goodAt".equals( editType )) {
                tvTitle.setText( "擅长" );
                if (UtilString.isEmpty( editData )) {
                    edtUserinfo.setHint( "请输入擅长" );
                }

                editTypeInt = 3;
            }
        }


    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_edit_userinfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        // TODO: add setContentView(...) invocation
        ButterKnife.bind( this );
    }

    @OnClick(R.id.tv_save)
    public void onViewClicked() {

        if (UtilString.isEmpty( edtUserinfo.getText().toString().trim() )) {
            ToastUtil.showToastCenter( mContext, "请输入相关内容" );
        } else {
            RestProxy.getInstance().saveUserInfo( editTypeInt, edtUserinfo.getText().toString().trim(), new Observer<BaseResponse>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(BaseResponse baseResponse) {
                    try {
                        Intent intent = new Intent();
                        intent.putExtra( "editData", edtUserinfo.getText().toString().trim() );
                        setResult( RESULT_OK, intent );
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onError(Throwable e) {
                    ToastUtil.onError( mContext, e );
                }

                @Override
                public void onComplete() {

                }
            } );
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
